package com.yljk.yljkproject.view.pickview;

import android.content.Context;
import android.view.View;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.CustomListener;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableMap;
import com.yilijk.base.utils.ALog;
import com.yljk.yljkproject.MyActivityManager;
import com.yljk.yljkproject.R;
import com.yljk.yljkproject.newpickview.PickViewCallback;
import com.yljk.yljkproject.newpickview.PickViewUtils;

import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;


public class PickerView {
    private static final String TAG = "AASSDD";
    private volatile static PickerView instance;
    private TimePickerView pvTime;
    private String mark = null;
    private String type = null;
    private String title = null;
    private List<String> list;

    private HashMap<String, Object> map = new HashMap<>();
    private HashMap<String, Object> maps = new HashMap<>();
    private ReadableArray data;

    private String returnString = null;

    private PickerView() {

    }

    //获取pickerview  对象
    public static PickerView getInstance(Context context) {
        if (instance == null) {
            instance = new PickerView();
            synchronized (PickerView.class) {
                if (instance == null) {
                    instance = new PickerView();
                }
            }
        }
        return instance;
    }

    //传值 获取  具体的pickerview  类型
    /*
     * data   json串
     * context  页面对象
     * view   需要传回选中的文字对象
     *
     * */

    public void getPicker(ReadableMap readableMap, Context context, Promise callback) {
        ALog.d(TAG, "getPicker: 这里" + readableMap);
        try {
            mark = readableMap.getString("mark");
            type = readableMap.getString("type");
            title = readableMap.getString("title");
            data = readableMap.getArray("data");
        }catch (Exception e){
            ALog.e("Error ALog:",e.getMessage());
        }
        
        ALog.d(TAG, "getPicker: mark ===" + mark + "-------type ====" + type + "-------title ====" + title);

//        if (type.equals("normal")) {  //判断类型  是时间选择  还是  选项选择
//            list.clear();
//            data = readableMap.getArray("data");
//            for (int i = 0; i < data.size(); i++) {
//                list.add(data.getString(i));
//            }
//            GetOptionPickView(context, callback);
//        } else
        if ("time".equals(type)) {
            boolean isMax = readableMap.getBoolean("isMaxDate");
            boolean isMin = readableMap.getBoolean("isMinDate");
            getTimePickerView(context, callback,isMax,isMin);
        }else{
            HashMap<String, Object> hashMap = new HashMap<>();
            list = new ArrayList<>();
            if (data != null && data.size() != 0) {
                for (int i = 0; i < data.size(); i++) {
                    list.add(data.getString(i));
                }
            } else {
                list = new ArrayList<>();
            }
            hashMap.put("data",list);
            PickViewUtils.getPickViewUtils().getPickViewShow(MyActivityManager.getInstance().getCurrentActivity(),
                    type, title,
                    "取消", "确定",
                    mark, hashMap, MyActivityManager.getInstance().getCurrentActivity().getWindow().getDecorView(), new PickViewCallback() {
                        @Override
                        public void getSureResult(String result) {
                            ALog.i("pickview", "getSureResult: " + result);
                            callback.resolve(result);
                        }
                    });
        }
    }

    //时间选择器
    /*
     * context  页面对象
     * mark  标记
     * column   列数
     * view  接收回传文字
     * */
    private void getTimePickerView(Context context, Promise view, boolean isMax, boolean isMin) {

        //创建数组  展示时间的列数  需要
        boolean[] columns = new boolean[]{true, true, true, false, false, false};
        String[] splits = null;
        Calendar selectedDate = Calendar.getInstance();//系统时间   有mark 为标记时间  无mark 为当前时间
        Calendar startDate = Calendar.getInstance();  //选择器起始时间
        Calendar endDate = Calendar.getInstance();//选择器最终时间     一般为系统当前时间
        if ( mark != null && !mark.isEmpty() ) {
            //分割标记 填入 对应的  时间  级别
            splits = mark.split("-");
            if (splits.length > 1) {    //预防  标记为空
                int year = Integer.parseInt(splits[0]);
                int month = Integer.parseInt(splits[1]);
                int day = Integer.parseInt(splits[2]);
                selectedDate.set(year, month-1, day);  //加入选择器
            }

        }

        int year = selectedDate.get(Calendar.YEAR);
        int month = selectedDate.get(Calendar.MONTH);
        int day = selectedDate.get(Calendar.DAY_OF_MONTH);

        if (isMax){
            startDate.set(year-100,month,day);   //设置起始时间时间
            endDate.set( year, month, day);

        }
        if (isMin){
            startDate.set(year,month,day);   //设置起始时间时间
            endDate.set( year+100, month, day);
        }
        if (!isMax && !isMin){
            startDate.set(year-100, month, day);   //设置起始时间时间
            endDate.set(year+100,month,day);
        }


//        endDate.set(2027, 2, 28);
        //时间选择器 ，自定义布局
        pvTime = new TimePickerBuilder(context, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {//选中事件回调
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");  //年月日
                String times = simpleDateFormat.format(date);  //解析

                maps.put("mark", times);
                maps.put("title", times);
                map.put("selInfo", maps);
                map.put("title", title);

                JSONObject jsonObject = new JSONObject(map);
                returnString = jsonObject.toString();
                view.resolve(returnString);


                view.resolve(times);  //回传 赋值
            }
        })
                .setType(columns)  //列数判断
                .setOutSideCancelable(true)//点击屏幕，点在控件外部范围时，是否取消显示
                .setDate(selectedDate)//设置 标记时间
                .setRangDate(startDate, endDate)   //设置  始末时间
                //获取layout 控件 和监听
                .setLayoutRes(R.layout.picker_time_layout, new CustomListener() {
                    @Override
                    public void customLayout(View v) {
                        final TextView tvSubmit = (TextView) v.findViewById(R.id.pickview_sure);
                        TextView ivCancel = (TextView) v.findViewById(R.id.pickview_cancel);
                        TextView titles = (TextView) v.findViewById(R.id.pickview_title);
                        //添加标题
                        titles.setText("时间");
                        //确认监听
                        tvSubmit.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                //回传数据
                                pvTime.returnData();
                                view.resolve(returnString);
                                //销毁
                                pvTime.dismiss();
                            }
                        });
                        //取消监听
                        ivCancel.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                //销毁
                                pvTime.dismiss();
                            }
                        });
                    }
                })
                .isCyclic(false)//是否循环滚动
                .setLabel("年", "月", "日", "时", "分", "秒")
                .setLineSpacingMultiplier(3.0f)    //选项间距
                .setTextXOffset(40, 0, -40, 0, 0, 0)   //列 之间的距离
                .isCenterLabel(false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
                .setDividerColor(0XFFF4F4F4) //选中选项上下的边框线
                .isDialog(false)   //是否为弹窗样式
                .setOutSideCancelable(true)//点击外部取消
                .build();  //构建
        pvTime.show();  //显示

    }





}
